package com.gec.controller;

import com.gec.domain.ProcessConfig;
import com.gec.service.WorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author Aniki
 * @date 2021-12-13
 */
@Controller
@RequestMapping("/WorkFlow")
public class WorkFlowController {
    @Autowired
    private WorkFlowService workFlowService;

    @RequestMapping("/deployProc")
    public String deployProc(
            @RequestParam("zipFile")MultipartFile file,
            @RequestParam("note")String note,
            @RequestParam("category")String category
            ){
         String id = makeUUID();
        ProcessConfig config = new ProcessConfig();
        config.setId(id);
        config.setNote(note);
        config.setProcCategory(category);
        String deployId;
        try {
            deployId =  workFlowService.createDeployment(config, file.getInputStream());

           return "redirect:/WorkFlow/viewProcList?deployId="+deployId;
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:/WorkFlow/viewUploadProc?errMsg=DeployFailed";
        }
    }

    private String makeUUID() {

        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        return uuid;
    }
        @RequestMapping("/viewUploadProc")
        public String viewUploadProc(){

              return "workflow/viewUploadProc";
    }
}
